package com.aaa.aop.service.aspect;

import com.aaa.aop.service.annotation.MyPermission;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/11/12  17:36
 */
/**
 * 自定义注解--aop
 */
public class PermissionAdive {
    //定义切点
    @Pointcut("@annotation(com.aaa.aop.service.annotation.MyPermission)")
    public void permissionPointcut(){}
    private List<String> permissions=new ArrayList<String>();
    public PermissionAdive(){
        permissions.add("list");
        permissions.add("delete");
        permissions.add("update");
    }
    @Around("permissionPointcut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        //得到连接点执行的方法对象
        MethodSignature methodSignatur= (MethodSignature) joinPoint.getSignature();
        Method method = methodSignatur.getMethod();
        //方法上的注解对象
        MyPermission annotation = method.getAnnotation(MyPermission.class);
        if(annotation!=null){
            //获取注解的属性value值
            String value = annotation.value();
            if(!permissions.contains(value)){
                return  "quanxianbuzu";
            }
        }

        return joinPoint.proceed();
    }
}
